import { LockOutlined, UserOutlined } from '@ant-design/icons';
import {
  LoginForm,
  ProConfigProvider,
  ProFormCheckbox,
  ProFormText,
} from '@ant-design/pro-components';
import { theme,message,ConfigProvider } from 'antd';
import { useEffect, type CSSProperties } from 'react';
import { history,useModel } from 'umi';
import services from '@/services/demo';

const { login,getUserInfo } =
  services.UserController;

const iconStyles: CSSProperties = {
  color: 'rgba(0, 0, 0, 0.2)',
  fontSize: '18px',
  verticalAlign: 'middle',
  cursor: 'pointer',
};
export default () => {
  const { token } = theme.useToken();
  const [messageApi, contextHolder] = message.useMessage();
  const {userInfo,setUserInfo} = useModel('global')// 数据流
 
  useEffect(()=>{
    const queryParams = new URLSearchParams(history.location.search)    // 得到URLSearchParams解析对象
    const result = Object.fromEntries(queryParams.entries())    // { ddd: 1, fff: 2 
    console.log(2,result)
    if(result.token){
      setCookie('satoken',result.token,1)
      localStorage.setItem('token',result.token)
      getInfo()
    }

  },[])
  const onFinish = async (values: any) => {
    login({
      ...values,
    }).then((res:any)=>{
      if(res.code=='200'){
        getInfo()
      }else{
        message.error(res.message);
      }
    })
  };
  const getInfo = ()=>{
    getUserInfo().then((res2:any)=>{
      if(res2.code=='200'){
        sessionStorage.setItem('userInfo',JSON.stringify(res2.data))
        setUserInfo(res2.data)
        if(res2.data.role == 'admin'){
          message.success('登录成功',1,()=>{
            if(res2.data.role == 'admin'){
              history.push('/announcement');
            }else{
              if(res2.data.isPartTimeJob==1){
                history.push('/uploadRecords');
              }else{
              }
            }
          });
        }else{
          message.error('您的账号无管理权限')
        }
      }
    })
  }
  const setCookie = (name:any, value:any, days:any)=> {
    let expires = '';
    if (days) {
      const date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = '; expires=' + date.toUTCString();
    }
    document.cookie = name + '=' + (value || '') + expires + '; path=/';
  }
  return (
    <ConfigProvider theme={{
      token: {
        colorPrimary: '#ffcc00',
        // borderRadius: data.borderRadius,
      },}}>
      <div >
        <LoginForm
          title="管理后台"
          containerStyle={{
            width: '100vw',
            height: '100vh',
            paddingTop: '100px',
            color: '#ffcc00',
            backgroundColor: '#fff',
            backdropFilter: 'blur(4px)',
          }}
          onFinish={onFinish}
        >
          <div style={{ paddingTop: '50px' }}>
            <ProFormText
              name="account"
              fieldProps={{
                size: 'large',
                prefix: <UserOutlined className={'prefixIcon'} />,
              }}
              placeholder={'请输入用户名'}
              rules={[
                {
                  required: true,
                  message: '请输入用户名!',
                },
              ]}
            />
            <ProFormText.Password
              name="password"
              fieldProps={{
                size: 'large',
                prefix: <LockOutlined className={'prefixIcon'} />,
              }}
              placeholder={'请输入密码'}
              rules={[
                {
                  required: true,
                  message: '请输入密码！',
                },
              ]}
            />
          </div>
        </LoginForm>
      </div>
    </ConfigProvider>
  );
};
